package com.wsc.wisdomchina.activitity.minemodule;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.ShakeActivity;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.activitity.wisdomParty.PartyApplyActivity;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.javaBean.BaoMingDetailBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.javaBean.wisdomParty.PartyApplyDetailBean;
import com.wsc.wisdomchina.presenter.ContributorPresenter;
import com.wsc.wisdomchina.view.CommomDialog;
import com.wsc.wisdomchina.view.MyToast;


public class BaoMingDetailActivity extends BaseActivity implements ContributorView, View.OnClickListener {

    private ImageView regist_back, iv_head;
    private TextView tv_headtitle, activity_wdfk_bxjdxq_slove, tv_title, tv_time, activity_wdfk_bxjdxq_time, tv_name, tv_sex, tv_phone, tv_cancel;
    private TextView et_remark;

    private LinearLayout mShakeLayout;
    private TextView mShakeCount;
    private View mShakeDivider;

    private ContributorPresenter contributorPresenter;
    private Context context;
    private final String DETAILFLAG = BaoMingDetailActivity.class.getSimpleName();
    private final String DELETEHOUSEDELETEFLAG = "DELETEHOUSEDELETEFLAG";
    private final String PARTY_DETAIL = "PARTY_DETAIL";
    private final String PARTY_DELETE = "PARTY_DELETE";
    private MyToast myToast;
    private String deleteId;
    private String acId;
    private String Frag_from = "";//标记从那个activity 跳转过来

    private String specilTitle = "年年重九胜春光，久久佳节送富康";
    private int SPECIL_ACTIVE = 0;
    private CommomDialog mDialog;
    private boolean isNeedFresh = false;
    private TextView mGoShake;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            showInputDialog();
        }
    };
    public  void showInputDialog() {
        //弹出提示框
        mDialog =  new CommomDialog(this, R.style.dialog, "", new CommomDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                Intent intent = new Intent(BaoMingDetailActivity.this, ShakeActivity.class);
                intent.putExtra("id",deleteId);
                intent.putExtra("acid",acId);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                isNeedFresh = true;
            }
        });
        if(mDialog.isShowing()){
         return;
        }
        mDialog.show();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.baomingdetail_layout);
        initView();
        context = this;
        myToast = new MyToast(context);
        contributorPresenter = new ContributorPresenter(this, context);
        Intent intent = getIntent();
        Frag_from = intent.getStringExtra("Frag");
        acId = intent.getStringExtra("acid");
        if (PartyApplyActivity.PARTY_APPLY.equals(Frag_from)) {
            deleteId = intent.getStringExtra("id");
            contributorPresenter.getParyApplyActivityDetail(deleteId, PARTY_DETAIL);
        } else {
            deleteId = intent.getStringExtra("id");
            contributorPresenter.getBuildingCommunityActivityDetail(deleteId, DETAILFLAG);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(isNeedFresh){
            if (PartyApplyActivity.PARTY_APPLY.equals(Frag_from)) {
                contributorPresenter.getParyApplyActivityDetail(deleteId, PARTY_DETAIL);
            } else {
                contributorPresenter.getBuildingCommunityActivityDetail(deleteId, DETAILFLAG);
            }
        }
        isNeedFresh = false;
    }

    private void initView() {
        activity_wdfk_bxjdxq_slove = (TextView) findViewById(R.id.activity_wdfk_bxjdxq_slove);//状态
        activity_wdfk_bxjdxq_time = (TextView) findViewById(R.id.activity_wdfk_bxjdxq_time);//报名时间
        regist_back = (ImageView) findViewById(R.id.regist_back);
        regist_back.setOnClickListener(this);
        tv_headtitle = (TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("报名详情");
        tv_headtitle.requestFocus();
        tv_headtitle.setFocusable(true);
        tv_headtitle.setFocusableInTouchMode(true);
        iv_head = (ImageView) findViewById(R.id.iv_head);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_sex = (TextView) findViewById(R.id.tv_sex);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_cancel = (TextView) findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(this);
        et_remark = (TextView) findViewById(R.id.et_remark);
        mGoShake = (TextView) findViewById(R.id.tv_go_shake);
        mGoShake.setOnClickListener(this);
        mShakeLayout = (LinearLayout) findViewById(R.id.ll_shake_layout);
        mShakeCount = (TextView) findViewById(R.id.tv_shake_count);
        mShakeDivider = findViewById(R.id.shake_divider);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.regist_back:
                finish();
                break;
            case R.id.tv_cancel:
                if (PartyApplyActivity.PARTY_APPLY.equals(Frag_from)) {
                    contributorPresenter.deletePartyapply(deleteId, PARTY_DELETE);
                } else {
                    contributorPresenter.getCommunityActivityDelete(deleteId, DELETEHOUSEDELETEFLAG);
                }
                break;
            case R.id.tv_go_shake:
                Intent intent = new Intent(BaoMingDetailActivity.this, ShakeActivity.class);
                intent.putExtra("id",deleteId);
                intent.putExtra("acid",acId);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                isNeedFresh = true;
                break;
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(DELETEHOUSEDELETEFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = (String) object;
                    if (jsonMessage.equals("41")) {
                        myToast.showShort("取消成功");
                        Intent intent = getIntent();
                        setResult(1, intent);
                        finish();
                    } else {
                        myToast.showShort("取消失败");
                    }
                } else {
                    myToast.showShort("取消失败");
                }
            } else if (flag.equals(DETAILFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    BaoMingDetailBean baoMingDetailBean = JsonTools.jsonStringToObject(jsonMessage, BaoMingDetailBean.class);
                    if (baoMingDetailBean != null) {
                        if (baoMingDetailBean.getState() == 0) {
                            activity_wdfk_bxjdxq_slove.setText("待审核");
                            tv_cancel.setVisibility(View.VISIBLE);
                            activity_wdfk_bxjdxq_slove.setTextColor(context.getResources().getColor(R.color.little_white));
                        } else if (baoMingDetailBean.getState() == 1) {
                            activity_wdfk_bxjdxq_slove.setText("报名成功");
                            activity_wdfk_bxjdxq_slove.setTextColor(Color.GREEN);
                            tv_cancel.setVisibility(View.GONE);
                        } else if (baoMingDetailBean.getState() == 2) {
                            activity_wdfk_bxjdxq_slove.setText("报名失败");
                            activity_wdfk_bxjdxq_slove.setTextColor(Color.RED);
                            tv_cancel.setVisibility(View.GONE);
                        }else{
                            tv_cancel.setVisibility(View.GONE);
                        }
                        //mHandler.sendEmptyMessageDelayed(SPECIL_ACTIVE,1000 *1);
                        boolean canShake = baoMingDetailBean.getSystemDate() >baoMingDetailBean.getActiveStartDate() && baoMingDetailBean.getSystemDate() < baoMingDetailBean.getActiveEndDate();
                        if(baoMingDetailBean.getState() == 1 && canShake && specilTitle.equals(baoMingDetailBean.getTitle())){
                           // mHandler.sendEmptyMessageDelayed(SPECIL_ACTIVE,1000 *3);
                            mGoShake.setVisibility(View.VISIBLE);
                        }else{
                            mGoShake.setVisibility(View.GONE);
                        }

                        String date_time = Tools.timedate(String.valueOf(baoMingDetailBean.getSignTime() / 1000));
                        activity_wdfk_bxjdxq_time.setText(date_time);

                        Glide.with(this).load(baoMingDetailBean.getGuideImg()).into(iv_head);

                        tv_title.setText(baoMingDetailBean.getTitle());
                        tv_time.setText(Tools.timedate(String.valueOf(baoMingDetailBean.getCreateDate() / 1000)));
                        tv_name.setText(baoMingDetailBean.getName());
                        if (baoMingDetailBean.getGender() == 0) {
                            tv_sex.setText("女");
                        } else {
                            tv_sex.setText("男");
                        }
                        tv_phone.setText(baoMingDetailBean.getPhone());
                        et_remark.setText(baoMingDetailBean.getRemark());
/*                        if(specilTitle.equals(baoMingDetailBean.getTitle())){
                            tv_cancel.setVisibility(View.GONE);
                        }else{
                            tv_cancel.setVisibility(View.VISIBLE);
                        }*/
/*                        if(specilTitle.equals(baoMingDetailBean.getTitle())){
                            mShakeDivider.setVisibility(View.VISIBLE);
                            mShakeLayout.setVisibility(View.VISIBLE);
                            mShakeCount.setText("" + baoMingDetailBean.getMaxRecord() + "次");
                        }else{
                            mShakeDivider.setVisibility(View.GONE);
                            mShakeLayout.setVisibility(View.GONE);
                        }*/
                    }
                }
            } else if (flag.equals(PARTY_DETAIL)) {
                PartyApplyDetailBean partyApplyDetailBean = JsonTools.jsonStringToObject(jsonString, PartyApplyDetailBean.class);
                if (partyApplyDetailBean != null && partyApplyDetailBean.getCode().equals("200")) {
                    PartyApplyDetailBean.MessageBean message = partyApplyDetailBean.getMessage();
                    if (message != null) {
                        if (message.getStatus() == 0) {
                            activity_wdfk_bxjdxq_slove.setText("待审核");
                            activity_wdfk_bxjdxq_slove.setTextColor(context.getResources().getColor(R.color.little_white));
                        } else if (message.getStatus() == 1) {
                            activity_wdfk_bxjdxq_slove.setText("报名成功");
                            activity_wdfk_bxjdxq_slove.setTextColor(Color.GREEN);
                        } else if (message.getStatus() == 2) {
                            activity_wdfk_bxjdxq_slove.setText("报名失败");
                            activity_wdfk_bxjdxq_slove.setTextColor(Color.RED);
                        }

                        String date_time = Tools.timedate(String.valueOf(message.getApply() / 1000));
                        activity_wdfk_bxjdxq_time.setText(date_time);

                        Glide.with(this).load(message.getPartyMemActivityInfo().getMainImg()).into(iv_head);

                        tv_title.setText(message.getPartyMemActivityInfo().getActivityName());
                        tv_time.setText(Tools.timedate(String.valueOf(message.getPartyMemActivityInfo().getCreateTime() / 1000)));
                        tv_name.setText(message.getMemName());
                        if ("0".equals(message.getSex())) {
                            tv_sex.setText("女");
                        } else {
                            tv_sex.setText("男");
                        }
                        tv_phone.setText(message.getContactNumber());
                        et_remark.setText(message.getRemark());
                    }
                }

            } else if (flag.equals(PARTY_DELETE)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = (String) object;
                    if (jsonMessage.equals("SUCCESS")) {
                        myToast.showShort("取消成功");
                        Intent intent = getIntent();
                        setResult(1, intent);
                        finish();
                    } else {
                        myToast.showShort("取消失败");
                    }
                } else {
                    myToast.showShort("取消失败");
                }
            }

        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }

}
